// pages/coupon-center/index.js
import config from '../../etc/config'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    toView: '',
    windowHeight: '',
    num:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.hideShareMenu()
    this.getDetail(options)
    this.data.isclick = options.isClick ? options.isClick : ''
    this.data.checkItems = options.checkItems ? JSON.parse(options.checkItems) : ''
    var that = this
    that.setData({
      type: options.type,
    })
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          windowHeight: res.windowHeight - 150
        })
      }
    })
  },
  getDetail(val) {
    let params = {
      shopId: val.shopId,
      typeId: val.typeId
    }
    let token = wx.getStorageSync("token");
    if (val.type === "3") {
      wx.setNavigationBarTitle({
        title: '品牌类目选择'
      })
      config._post('/category/getDetails', token, params).then(res => {
        this.setData({
          dataList: res.data
        })
        if (this.data.checkItems) {
          // 设置已经选中的分类
          let checkItems = this.data.checkItems
          let dataList = this.data.dataList
          for (let i = 0; i < checkItems.length; i++) {
            for (let j = 0; j < dataList.length; j++) {
              if (checkItems[i].categoryId == dataList[j].categorie.id) {
                let checkBrand = checkItems[i].brand
                let brandList = dataList[j].brandList
                for (let k = 0; k < checkBrand.length; k++) {
                  for (let l = 0; l < brandList.length; l++) {
                    if (checkBrand[k].brandId == brandList[l].id) {
                      let index = "dataList[" + j + '].brandList[' + l + "].ischecked"
                      this.setData({
                        [index]: true
                      })
                    }
                  }
                }
              }
            }
          }
        }
      })
    } else {
      wx.setNavigationBarTitle({
        title: '洗护服务'
      })
      config._post('/category/getLaundryCate', token, params).then(res => {
        wx.hideLoading()
        this.setData({
          dataList: res.data
        })
        if (this.data.checkItems) {
          // 设置已经选中的分类
          let checkItems = this.data.checkItems
          let dataList = this.data.dataList
          for (let i = 0; i < checkItems.length; i++) {
            for (let j = 0; j < dataList.length; j++) {
              if (checkItems[i].categoryId == dataList[j].categorie.id) {
                let checkBrand = checkItems[i].brand
                let brandList = dataList[j].brandList
                for (let k = 0; k < checkBrand.length; k++) {
                  for (let l = 0; l < brandList.length; l++) {
                    if (checkBrand[k].brandId == brandList[l].id) {
                      // dataList[j].brandList[l].ischecked = true
                      let index = "dataList[" + j + '].brandList[' + l + "].ischecked"
                      let num = "dataList[" + j + '].brandList[' + l + "].num"
                      this.setData({
                        [index]: true,
                        [num]: checkBrand[k].num
                      })
                    }
                  }
                }
              }
            }
          }
        }
      })
    }
  },

  changeCate(e) {
    this.setData({
      current: e.target.dataset.index
    })
  },

  brandCheck(e) {
    var idx = e.currentTarget.dataset.index
    if (this.data.isclick) {
      for (let i = 0; i < this.data.dataList.length; i++) {
        for (let j = 0; j < this.data.dataList[i].brandList.length; j++) {
          this.data.dataList[i].brandList[j].ischecked = false
        }
      }
      this.data.dataList[this.data.current].brandList[idx].ischecked = true
      this.setData({
        dataList: this.data.dataList
      })
    } else {
      if (this.data.dataList[this.data.current].brandList[idx].ischecked == undefined) {
        this.data.dataList[this.data.current].brandList[idx].ischecked = false
      }
      var val = !this.data.dataList[this.data.current].brandList[idx].ischecked
      var index = "dataList[" + this.data.current + '].brandList[' + idx + "].ischecked"
      this.setData({
        [index]: val
      })
    }
  },

  submit() {
    let checkItems = []
    let options = {}
    for (let i = 0; i < this.data.dataList.length; i++) {
      options = {
        categoryId: this.data.dataList[i].categorie.id,
        category: this.data.dataList[i].categorie.name,
        brand: []
      }
      for (let j = 0; j < this.data.dataList[i].brandList.length; j++) {
        if (this.data.dataList[i].brandList[j].ischecked) {
          options.brand.push({
            brandId: this.data.dataList[i].brandList[j].id,
            name: this.data.dataList[i].brandList[j].name,
            num: this.data.dataList[i].brandList[j].num
          })
        }
      }
      if (options.brand.length !== 0) {
        checkItems.push(options)
      }
    }
    var pages = getCurrentPages(); // 获取页面栈
    var prevPage = pages[pages.length - 2]; // 上一个页面
    prevPage.setData({
      checkItems: checkItems
    })
    wx.navigateBack()
  },

  subtractNum(e){
    var val = this.data.dataList[this.data.current].brandList[e.currentTarget.dataset.index].num - 1
    if(val<=0){
      val = 0
      var ischecked = "dataList[" + this.data.current + '].brandList[' + e.currentTarget.dataset.index + "].ischecked"
      this.setData({
        [ischecked]: false
      })
    }
    var index = "dataList[" + this.data.current + '].brandList[' + e.currentTarget.dataset.index + "].num"
    this.setData({
      [index]: val
    })
  },
  addNum(e){
    if (this.data.dataList[this.data.current].brandList[e.currentTarget.dataset.index].num == undefined) {
      this.data.dataList[this.data.current].brandList[e.currentTarget.dataset.index].num = 0
    }
    var val = this.data.dataList[this.data.current].brandList[e.currentTarget.dataset.index].num + 1
    var num = "dataList[" + this.data.current + '].brandList[' + e.currentTarget.dataset.index + "].num"
    var ischecked = "dataList[" + this.data.current + '].brandList[' + e.currentTarget.dataset.index + "].ischecked"
    this.setData({
      [num]: val,
      [ischecked]: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})